package org.kukker.activity.widget;
import org.kukker.app.KukkerApp;
import java.util.Iterator;
import java.util.Map;
import org.kukker.R;
import org.kukker.event.Event;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.text.method.InputMethod;
import android.text.method.QwertyInputMethod;
import android.text.method.TextInputMethod;
import android.text.method.TextInputMethod.Capitalize;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.TableRow;
import android.widget.TextView;

public class KukkerEditText extends EditText {
	public String debugtag = "eventflow1";

	public KukkerEditText(Context context, AttributeSet attrs, Map inflateParams) {
		super(context, attrs, inflateParams);
		this.init();//set default Attributes
		this.setAttributesFromXml(attrs);
	}


	enum State {
		SELECTED, HOVERED, UNSELECTED;
	}

	private State mState = State.UNSELECTED;

/*
	public void setEditable(boolean editable) {
		this.mEditable = editable;

		if (editable) {

			this.setCursorVisible(true);
		} else {

			this.setCursorVisible(false);
		}
	}
	*/

	public boolean onTouchEvent(MotionEvent event) {
		//note:  to have default editing behavior, invoke only once either in onKeyUp or onKeyDown, if we invoke this on both key-up and key-down , we would have two characters
		boolean res = super.onTouchEvent(event); 

		String actionName = org.kukker.Util.kMotionEvent.getActionName(event);
		Log.i(debugtag, "[" + this.getClass().getSimpleName() + "]" +

		"[" + "isSelected:" + this.isSelected() + 
		", isFocused:" + this.isFocused() +
		"]"

		+ "[" + "onTouchEvent()"

		+ ", action:" + actionName + "]"

		);

		return res;
	}
/*
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		//	note:  to have default editing behavior, invoke only once either in onKeyUp or onKeyDown, if we invoke this on both key-up and key-down , we would have two characters
		
			Log.i(debugtag, "[" + this.getClass().getSimpleName() + "]" +

			"[" + "isSelected:" + this.isSelected() + "]"

			+ "[" + "onKeyUp() " + ",keyCode:" + keyCode

			+ "]");
			
			if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
				View v = this.focusSearch(View.FOCUS_DOWN);
				if(v != null){
					v.requestFocus();
				}			
				return true;
			}
			else if(keyCode == KeyEvent.KEYCODE_DPAD_UP){
				View v = this.focusSearch(View.FOCUS_UP);
				if(v != null){
					v.requestFocus();
				}
				//v.setSelected(true);
				return true;
			}else{
				
				boolean res = super.onKeyUp(keyCode, event); 
				return res;
			}			
	}
*/
	/*
	public boolean onKeyDown(int keyCode, KeyEvent event) {

			//boolean res = super.onKeyDown(keyCode, event);

			Log.i(debugtag, "[" + this.getClass().getSimpleName() + "]" +

			"[" + "isSelected:" + this.isSelected() + "]"

			+ "[" + "onKeyUp() " + ",keyCode:" + keyCode

			+ "]");
			
			return true;
		
	}
*/
	public void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
		Log.i(debugtag,

		"[" + this.getClass().getSimpleName() + "]" +

		"[" +
		"isSelected:" + this.isSelected() +  
		", isFocused:" + this.isFocused() +		
		"]"

		+ "[" + "onFocusChanged()" + ", gainFocus:" + gainFocus
				+ ", direction:" + direction + "]"

		);

		if (gainFocus) {
			this.setBackground(R.drawable.orangebutton_alpha);
			this.setSelected(true);
			this.requestFocus();
		} else {
			this.setBackground(R.drawable.greybutton_alpha);
			this.setSelected(false);
			this.clearFocus();
		}

	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event){		
		boolean res = super.onKeyDown(keyCode, event); //important
		int count = event.getRepeatCount();
		
		Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			"isSelected:"+this.isSelected()+
    			", isFocused:"+this.isFocused()+
    			"]"
    			
    			+"["
    			+"onKeyDown() " +
    			",keyCode:"+keyCode
    			
    			+"]"    			
    	);
		
		if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
			
			
			if(count > 5){ //auto move cursor to first character 
				this.setSelection(0);
			}
			
			//since we want to detect on key up later we have to stop this event from propagating.
			//stop propagating 
			return true;
		}
		
		else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
			if(count > 5){ //auto move cursor to first character
				int len = this.getText().length();
				this.setSelection(len);
			}
			
			
			//since we want to detect on key up later we have to stop this event from propagating.
			//so that the next keyup event will be handled by this object.
			return true;
		}
		
		else if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){	
			//since we want to detect on key up later we have to stop this event from propagating.
			//so that the next keyup event will be handled by this object.
			return true;		
		}
		else if(keyCode == KeyEvent.KEYCODE_SPACE){	
			return true;
		}
		
		return false;
		
    	
	}
	
	
	public boolean onKeyUp(int keyCode, KeyEvent event){		
		boolean res = super.onKeyUp(keyCode, event);
		return res;
	}
	

	

	private void init() {
		this.setTextSize(16);
		this.setHintTextColor(Color.GRAY);
		this.setPadding(0, 0, 0, 0);
		// this.setIncludeFontPadding(false);
		this.setBackground(R.drawable.greybutton_alpha);
		this.setHint("enter value");
		this.setTextColor(Color.BLACK);
		this.setPadding(3, 3, 3, 3);
		this.setFocusableInTouchMode(true);
		this.setClickable(true);
		this.setFocusable(true);		
		
		this.setInputMethod(new QwertyInputMethod(Capitalize.NONE,true));
	}
	
	
	/**
	 * 
	 * @param inflateParams
	 */
	private void setAttributesFromXml(AttributeSet attrs){
		//for(int num =attrs.getAttributeCount(),i=0;i<num;i++){
			
    		String hint = attrs.getAttributeValue(KukkerApp.NAMESPACE_ANDROID,"hint");
    		if(hint != null){this.setHint(hint);}
    		
    		String text = attrs.getAttributeValue(KukkerApp.NAMESPACE_ANDROID,"text");
    		if(text != null){this.setText(text);}
    		
    	//}   
	}
}